package GM.diagram.edit.parts;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import GM.diagram.part.GMVisualIDRegistry;

/**
 * @generated
 */
public class GMEditPartFactory implements EditPartFactory {

	/**
	 * @generated
	 */
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof View) {
			View view = (View) model;
			switch (GMVisualIDRegistry.getVisualID(view)) {

			case GoalDiagramEditPart.VISUAL_ID:
				return new GoalDiagramEditPart(view);

			case GoalEditPart.VISUAL_ID:
				return new GoalEditPart(view);

			case GoalNameEditPart.VISUAL_ID:
				return new GoalNameEditPart(view);

			case AdGoalEditPart.VISUAL_ID:
				return new AdGoalEditPart(view);

			case AdGoalNamePriorityEditPart.VISUAL_ID:
				return new AdGoalNamePriorityEditPart(view);

			case OperationEditPart.VISUAL_ID:
				return new OperationEditPart(view);

			case OperationNameEditPart.VISUAL_ID:
				return new OperationNameEditPart(view);

			case EntityEditPart.VISUAL_ID:
				return new EntityEditPart(view);

			case EntityNameEditPart.VISUAL_ID:
				return new EntityNameEditPart(view);

			case EventEditPart.VISUAL_ID:
				return new EventEditPart(view);

			case EventNameEditPart.VISUAL_ID:
				return new EventNameEditPart(view);

			case ActionEditPart.VISUAL_ID:
				return new ActionEditPart(view);

			case ActionActionTypeEditPart.VISUAL_ID:
				return new ActionActionTypeEditPart(view);

			case AgentEditPart.VISUAL_ID:
				return new AgentEditPart(view);

			case AgentNameEditPart.VISUAL_ID:
				return new AgentNameEditPart(view);

			case RefinementLinkEditPart.VISUAL_ID:
				return new RefinementLinkEditPart(view);

			case RefinementLinkTypeEditPart.VISUAL_ID:
				return new RefinementLinkTypeEditPart(view);

			case DefinitionEditPart.VISUAL_ID:
				return new DefinitionEditPart(view);

			case DefinitionDefEditPart.VISUAL_ID:
				return new DefinitionDefEditPart(view);

			case MappingEditPart.VISUAL_ID:
				return new MappingEditPart(view);

			case MappingNameEditPart.VISUAL_ID:
				return new MappingNameEditPart(view);

			case Definition2EditPart.VISUAL_ID:
				return new Definition2EditPart(view);

			case DefinitionDef2EditPart.VISUAL_ID:
				return new DefinitionDef2EditPart(view);

			case Mapping2EditPart.VISUAL_ID:
				return new Mapping2EditPart(view);

			case MappingName2EditPart.VISUAL_ID:
				return new MappingName2EditPart(view);

			case Definition3EditPart.VISUAL_ID:
				return new Definition3EditPart(view);

			case DefinitionDef3EditPart.VISUAL_ID:
				return new DefinitionDef3EditPart(view);

			case Mapping3EditPart.VISUAL_ID:
				return new Mapping3EditPart(view);

			case MappingName3EditPart.VISUAL_ID:
				return new MappingName3EditPart(view);

			case Definition4EditPart.VISUAL_ID:
				return new Definition4EditPart(view);

			case DefinitionDef4EditPart.VISUAL_ID:
				return new DefinitionDef4EditPart(view);

			case Mapping4EditPart.VISUAL_ID:
				return new Mapping4EditPart(view);

			case MappingName4EditPart.VISUAL_ID:
				return new MappingName4EditPart(view);

			case Definition5EditPart.VISUAL_ID:
				return new Definition5EditPart(view);

			case DefinitionDef5EditPart.VISUAL_ID:
				return new DefinitionDef5EditPart(view);

			case Mapping5EditPart.VISUAL_ID:
				return new Mapping5EditPart(view);

			case MappingName5EditPart.VISUAL_ID:
				return new MappingName5EditPart(view);

			case Definition6EditPart.VISUAL_ID:
				return new Definition6EditPart(view);

			case DefinitionDef6EditPart.VISUAL_ID:
				return new DefinitionDef6EditPart(view);

			case Mapping6EditPart.VISUAL_ID:
				return new Mapping6EditPart(view);

			case MappingName6EditPart.VISUAL_ID:
				return new MappingName6EditPart(view);

			case Definition7EditPart.VISUAL_ID:
				return new Definition7EditPart(view);

			case DefinitionDef7EditPart.VISUAL_ID:
				return new DefinitionDef7EditPart(view);

			case Mapping7EditPart.VISUAL_ID:
				return new Mapping7EditPart(view);

			case MappingName7EditPart.VISUAL_ID:
				return new MappingName7EditPart(view);

			case Definition8EditPart.VISUAL_ID:
				return new Definition8EditPart(view);

			case DefinitionDef8EditPart.VISUAL_ID:
				return new DefinitionDef8EditPart(view);

			case Mapping8EditPart.VISUAL_ID:
				return new Mapping8EditPart(view);

			case MappingName8EditPart.VISUAL_ID:
				return new MappingName8EditPart(view);

			case AttributeEditPart.VISUAL_ID:
				return new AttributeEditPart(view);

			case AttributeMultiplicityNameTypeEditPart.VISUAL_ID:
				return new AttributeMultiplicityNameTypeEditPart(view);

			case ReferenceEditPart.VISUAL_ID:
				return new ReferenceEditPart(view);

			case ReferenceMultiplicityNameEditPart.VISUAL_ID:
				return new ReferenceMultiplicityNameEditPart(view);

			case StateEditPart.VISUAL_ID:
				return new StateEditPart(view);

			case StateNameEditPart.VISUAL_ID:
				return new StateNameEditPart(view);

			case Attribute2EditPart.VISUAL_ID:
				return new Attribute2EditPart(view);

			case AttributeMultiplicityNameType2EditPart.VISUAL_ID:
				return new AttributeMultiplicityNameType2EditPart(view);

			case Reference2EditPart.VISUAL_ID:
				return new Reference2EditPart(view);

			case ReferenceMultiplicityName2EditPart.VISUAL_ID:
				return new ReferenceMultiplicityName2EditPart(view);

			case GoalLtlCompartmentEditPart.VISUAL_ID:
				return new GoalLtlCompartmentEditPart(view);

			case DefinitionMappingCompartmentEditPart.VISUAL_ID:
				return new DefinitionMappingCompartmentEditPart(view);

			case AdGoalTriggerAdGoalCompartmentEditPart.VISUAL_ID:
				return new AdGoalTriggerAdGoalCompartmentEditPart(view);

			case AdGoalConditionAdGoalCompartmentEditPart.VISUAL_ID:
				return new AdGoalConditionAdGoalCompartmentEditPart(view);

			case DefinitionMappingCompartment3EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment3EditPart(view);

			case DefinitionMappingCompartment4EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment4EditPart(view);

			case OperationDompostCompartmentEditPart.VISUAL_ID:
				return new OperationDompostCompartmentEditPart(view);

			case OperationDompreCompartmentEditPart.VISUAL_ID:
				return new OperationDompreCompartmentEditPart(view);

			case OperationReqpostCompartmentEditPart.VISUAL_ID:
				return new OperationReqpostCompartmentEditPart(view);

			case OperationReqpreCompartmentEditPart.VISUAL_ID:
				return new OperationReqpreCompartmentEditPart(view);

			case OperationTrigpreCompartmentEditPart.VISUAL_ID:
				return new OperationTrigpreCompartmentEditPart(view);

			case DefinitionMappingCompartment2EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment2EditPart(view);

			case DefinitionMappingCompartment5EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment5EditPart(view);

			case DefinitionMappingCompartment6EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment6EditPart(view);

			case DefinitionMappingCompartment7EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment7EditPart(view);

			case DefinitionMappingCompartment8EditPart.VISUAL_ID:
				return new DefinitionMappingCompartment8EditPart(view);

			case EntityStateCompartmentEditPart.VISUAL_ID:
				return new EntityStateCompartmentEditPart(view);

			case EntityAttributeCompartmentEditPart.VISUAL_ID:
				return new EntityAttributeCompartmentEditPart(view);

			case EntityRefCompartEditPart.VISUAL_ID:
				return new EntityRefCompartEditPart(view);

			case EventAttrEntityCompartEditPart.VISUAL_ID:
				return new EventAttrEntityCompartEditPart(view);

			case EventParamCompartsEditPart.VISUAL_ID:
				return new EventParamCompartsEditPart(view);

			case AdGoalHelpEditPart.VISUAL_ID:
				return new AdGoalHelpEditPart(view);

			case AgentOperatEditPart.VISUAL_ID:
				return new AgentOperatEditPart(view);

			case GoalRefEditPart.VISUAL_ID:
				return new GoalRefEditPart(view);

			case RefinementLinkTargetEditPart.VISUAL_ID:
				return new RefinementLinkTargetEditPart(view);

			case GoalOperatEditPart.VISUAL_ID:
				return new GoalOperatEditPart(view);

			case AdGoalActionsEditPart.VISUAL_ID:
				return new AdGoalActionsEditPart(view);

			case ContributionLinkEditPart.VISUAL_ID:
				return new ContributionLinkEditPart(view);

			case ContributionLinkWeightEditPart.VISUAL_ID:
				return new ContributionLinkWeightEditPart(view);

			case ReferenceTypeEditPart.VISUAL_ID:
				return new ReferenceTypeEditPart(view);

			}
		}
		return createUnrecognizedEditPart(context, model);
	}

	/**
	 * @generated
	 */
	private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
		// Handle creation of unrecognized child node EditParts here
		return null;
	}

	/**
	 * @generated
	 */
	public static CellEditorLocator getTextCellEditorLocator(
			ITextAwareEditPart source) {
		if (source.getFigure() instanceof WrappingLabel)
			return new TextCellEditorLocator((WrappingLabel) source.getFigure());
		else {
			return new LabelCellEditorLocator((Label) source.getFigure());
		}
	}

	/**
	 * @generated
	 */
	static private class TextCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private WrappingLabel wrapLabel;

		/**
		 * @generated
		 */
		public TextCellEditorLocator(WrappingLabel wrapLabel) {
			this.wrapLabel = wrapLabel;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getWrapLabel() {
			return wrapLabel;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getWrapLabel().getTextBounds().getCopy();
			getWrapLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				if (getWrapLabel().isTextWrapOn()
						&& getWrapLabel().getText().length() > 0) {
					rect.setSize(new Dimension(text.computeSize(rect.width,
							SWT.DEFAULT)));
				} else {
					int avr = FigureUtilities.getFontMetrics(text.getFont())
							.getAverageCharWidth();
					rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
							SWT.DEFAULT)).expand(avr * 2, 0));
				}
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	/**
	 * @generated
	 */
	private static class LabelCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private Label label;

		/**
		 * @generated
		 */
		public LabelCellEditorLocator(Label label) {
			this.label = label;
		}

		/**
		 * @generated
		 */
		public Label getLabel() {
			return label;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getLabel().getTextBounds().getCopy();
			getLabel().translateToAbsolute(rect);
			if (!text.getFont().isDisposed()) {
				int avr = FigureUtilities.getFontMetrics(text.getFont())
						.getAverageCharWidth();
				rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
						SWT.DEFAULT)).expand(avr * 2, 0));
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}
}
